<?php

namespace app\index\controller;

use app\admin\service\OssService;
use app\BaseController;
use app\index\diyclass\Interal;
use app\index\model\CommentBg as CommentBgModal;
use app\index\model\Buy as BuyModal;
use app\index\model\UserModel;
use think\facade\Db;
use think\Request;

class CommentBg  extends BaseController
{
    protected $middleware = [
        'Auth' => ['except' => []],
    ];
    public function buyList(Request $request)
    {
        $sql = new BuyModal();
        $whereArr[] = ['userId', '=', $request->user->id];
        $whereArr[] = ['type', '=', 1];
        $list = $sql->where($whereArr)->with('info')->select();
        foreach ($list as $key => $value) {
            $list[$key] = $value['info'];
        }
        $oss = new OssService();
        return result()->success($oss->signIds($list, 'image_id', 'image'));
    }

    public function list(Request $request)
    {
        $params = $this->request->post(['current', 'size']);
        $sql = new CommentBgModal();
        $whereArr[] = ['type', '=', 1];
        $haswhereArr[] = ['userId', '=', $request->user->id];
        $haswhereArr[] = ['type', '=', 1];
        $list = $sql->where($whereArr)->where('id', 'NOT IN', function ($query) use($haswhereArr) {
            $query->table('car_buy')->where($haswhereArr)->field('aboutId');
        })->page($params['current'])->limit($params['size'])->order('sort desc')->order('create_at desc')->select();
        $oss = new OssService();
        return result()->success($oss->signIds($list, 'image_id', 'image'));
    }

    public function buy(Request $request)
    {
        $params = $this->request->post(['aboutId']);
        $whereArr[] = ['userId', '=', $request->user->id];
        $whereArr[] = ['aboutId', '=', $params['aboutId']];
        $bgSql = new CommentBgModal();
        $info = $bgSql->where('id', $params['aboutId'])->findOrEmpty();
        if ($info->isEmpty()) {
            return result()->fail("背景不存在", 400);
        }
        $sql = new BuyModal();
        $buyInfo = $sql->where($whereArr)->findOrEmpty();
        if(!$buyInfo->isEmpty()) {
            return result()->fail("已购买", 500);
        }
        if($info->price > $request->user->integral) {
            return result()->fail("软妹币不足", 400);
        }
        Db::startTrans();
        try {
            $sql->save([
                'userId' => $request->user->id,
                'aboutId' => $params['aboutId'],
                'type' => $info->type,
            ]);
            $userLog = new Interal($request->user->id,'BUY_COMMENT_BUY');
            $userLog->setNum(-$info->price);
            $userLog->setContent("购买背景");
            $userLog->setAboutId($params['aboutId']);
            $userLog->save();
            Db::commit();
            return result()->success("购买成功");
        }   catch (\Throwable $th) {
            Db::rollback();
            return result()->fail($th->getMessage(), 500);
        }
    }

    public function setBg(Request $request)
    {
        $params = $this->request->post(['id']);
        $sql = new UserModel();
        $sql->update([
            'comment_bg_id' => $params['id']
        ], ['id' => $request->user->id]);
        return result()->success("修改成功");
    }
}